<!--
 * @Description: 
 * @Version: V1.0.0
 * @Author: 周艳凯 484894717@qq.com
 * @Date: 2024-11-22 17:50:41
 * @LastEditors: 周艳凯 484894717@qq.com
 * @LastEditTime: 2024-11-25 11:33:51
 * @FilePath: index.vue
 * Copyright 2024 Marvin, All Rights Reserved. 
 * 2024-11-22 17:50:41
-->
<template>
  <view @click="add"> 我要上传图片了 </view>
</template>

<script lang="ts">
import { defineComponent, ref } from "vue";
import { chooseFile } from "./utils";

export default defineComponent({
  name: "Upload",
  props: {
    accept: {
      type: String,
      default: "image",
    },
    multiple: {
      type: Boolean,
      default: false,
    },
    maxSize: {
      type: Number,
      default: 1024 * 1024 * 2,
    },
    limit: Number,
    // 选择文件的来源
    sourceType: {
      type: Array,
      default: () => ["album", "camera"],
    },
    sizeType: {
      type: Array,
      // #ifndef MP-DINGTALK
      default: () => ["original", "compressed"],
      // #endif
    },
    compressed: {
      type: Boolean,
      default: true,
    },
    maxDuration: {
      type: Number,
      default: 60,
    },
    camera: {
      type: String,
      default: "back",
    },
    beforeUpload: {
      type: Function,
      default() {
        return true;
      },
    },
    fileList: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  setup(props) {
    const uploadFiles = ref([]);

    function add() {
      const {
        multiple,
        accept,
        sizeType,
        limit,
        sourceType,
        compressed,
        maxDuration,
        camera,
      } = props;
      // 文件选择
      chooseFile({
        multiple,
        sizeType,
        sourceType,
        maxCount: limit ? limit - uploadFiles.value.length : 9,
        accept,
        compressed,
        maxDuration,
        camera,
      })
        .then((res) => {
          console.log(res, "我是res");
          // 成功选择初始化file
          // let files = res;
          // 单选只有一个
          // if (!multiple) {
          //   files = files.slice(0, 1);
          // }
          // // 遍历列表逐个初始化上传参数
          // const mapFiles = async (files: ChooseFile[]) => {
          //   for (let index = 0; index < files.length; index++) {
          //     const file = files[index];
          //     if (file.type === "image" && !file.size) {
          //       const imageInfo = await getImageInfo(file.path);
          //       file.size = imageInfo.width * imageInfo.height;
          //     }
          //     Number(file.size) <= maxSize
          //       ? initFile(file)
          //       : emit("oversize", { file });
          //   }
          // };
          // // 上传前的钩子
          // if (beforeUpload) {
          //   beforeUpload({
          //     files,
          //     fileList: uploadFiles.value,
          //     resolve: (isPass: boolean) => {
          //       isPass && mapFiles(files);
          //     },
          //   });
          // } else {
          //   mapFiles(files);
          // }
        })
        .catch((error) => {
          console.log(error, "我是错误");
          // emit("chooseerror", { error });
        });
    }

    return { add };
  },
});
</script>
